<?php

namespace app\common\model;

use think\Db;
use think\Model;
use think\Request;


class Ad extends Model
{

    

    

    // 表名
    protected $name = 'ad';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = false;

    // 定义时间戳字段名
    protected $createTime = false;
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'start_time_text',
        'end_time_text',
	    'position_text',
	    'type_text',
	    'area_text'
    ];
    
    const POSITION_INDEX_HOME = 1;
    const POSITION_INDEX_CATE = 2;
    const POSITION_INDEX_RECOMMEND = 3;
    const POSITION_CATE = 4;
    const POSITION_INDEX_ACTIVE = 5;
    
    public static $positionList = [
        self::POSITION_INDEX_HOME => '首页顶部轮播图（690*247）',
	    self::POSITION_INDEX_CATE => '首页主题（337*180）',
	    self::POSITION_INDEX_RECOMMEND => '首页加盟背景(750*950)',
	    self::POSITION_INDEX_ACTIVE => '首页活动(600*335)',
//	    self::POSITION_CATE => '大赛页面(690*450)'
    ];
    
    public function getPositionTextAttr($value, $data) {
    	$value = $value ? : $data['position'];
    	$list = self::$positionList;
    	return $list[$value] ?? '';
    }
	
	public function getAreaTextAttr($value, $data) {
		$value = $value ? : $data['area'];
		$list = \app\admin\model\User::$areaList;
		return $list[$value] ??'';
	}
	
	const TYPE_GOODS = 1;
    const TYPE_PLAN = 2;
    const TYPE_TUTOR = 3;
    const TYPE_AGENT_ABOUT = 5;
	
	public static $typeList = [
		self::TYPE_GOODS => '商品详情',
		self::TYPE_PLAN => '创业大赛',
		self::TYPE_TUTOR => '导师大赛',
		self::TYPE_AGENT_ABOUT => '加盟介绍'
	];
	
	public function getTypeTextAttr($value, $data) {
		$value = $value ? : $data['jump_type'];
		$list = self::$typeList;
		return $list[$value] ?? '';
	}
	
    protected static function init()
    {
        self::afterInsert(function ($row) {
            $pk = $row->getPk();
            $row->getQuery()->where($pk, $row[$pk])->update(['sort' => $row[$pk]]);
        });
    }

    



    public function getStartTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['start_time']) ? $data['start_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getEndTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['end_time']) ? $data['end_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setStartTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setEndTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

	public function getBannerById($id) {
		$time = time();
		$where = [
			'area' => Request::instance()->header('area') ? : \app\admin\model\User::AREA_OTHER,
			'position' => $id,
			'start_time' => ['elt', $time],
			'end_time' => ['egt', $time],
		];
    	$rows = Db::name('ad')
		    ->where($where)
		    ->field('id, name, image, jump_url as params, background, jump_type, spec_id')
		    ->order(['sort' => 'asc'])
		    ->select();
    	
    	return $rows;
	}
}
